﻿// -----------------------------------------------------------------------
// <copyright file="PingPong.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ping
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class PingPong : sinetd.Services.ServiceWrapper
    {
        public PingPong(sinetd.Config cfg)
            : base("Ping", cfg.GetString("protocol"))
        {
            if (Protocol != null)
                Protocol.Config = "host=" + cfg.GetString("host") + ";port=" + cfg.GetString("port") + ";isServer=false";
        }

        public string Ping(int count)
        {
            string wrong = "Host doesn't answer.";
            if (Protocol.Connect())
            {
                Protocol.Send((string)InvokeCall("Ping", new object[] { count, null }));
                try
                {
                    string ans = Protocol.Recv();
                    Protocol.Close();
                    return ans;
                }
                catch
                {
                    Protocol.Close();
                    return wrong;
                }
            }
            else
            {
                return wrong;
            }
        }
    }
}
